package org.one.stone.soup.grfxML.plugin.image;

import java.awt.Image;
import java.io.IOException;

import org.one.stone.soup.grfxML.DataImage;
import org.one.stone.soup.grfxML.DataPoint;
import org.one.stone.soup.grfxML.DataStore;
import org.one.stone.soup.grfxML.DataString;
import org.one.stone.soup.grfxML.DataTranslation;
import org.one.stone.soup.grfxML.GeometryImage;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.TagModifier;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;

public class Cutter extends SimplePlugin {

	public static final int ARG_IMAGE_FILE = 0;

	public static final int ARG_TARGETS = 1;

	DataString imageFile = new DataString(TagModifier.STRING_TYPE_FILE);
	DataStore targets;

/**
 * SimpleImageButton constructor comment.
 */
public Cutter (GrfxMLEngine engine){
	super(engine);

	targets = new DataStore(engine);
}
/**
 * initialize method comment.
 */
public void initialize()
{
	Image source = loadImage( imageFile.getValue() );
	DataTranslation t = new DataTranslation();

	System.out.println("Cutting");

	for(int loop=0;loop<targets.size();loop+=2)
	{
		if( targets.get(loop) instanceof DataPoint && targets.get(loop+1) instanceof DataImage )
		{
			DataPoint cut = (DataPoint)targets.get(loop);
			DataImage image = (DataImage)targets.get(loop+1);

			image.translate(t);

			((GeometryImage)image.getGeometry(getEngine())).setImage(
					org.one.stone.soup.grfx.ImageFactory.cutImage(
							source,
							cut.getValueXMin(),
							cut.getValueYMin(),
							cut.getValueXMax(),
							cut.getValueYMax()
						)
				);
			System.out.println("Cut "+
							cut.getValueXMin()+","+
							cut.getValueYMin()+","+
							cut.getValueXMax()+","+
							cut.getValueYMax()
						);
		}
	}
}
/**
 *
 * @return java.awt.Image
 */
public java.awt.Image loadImage(String fileName)
{
		Image newImage = null;
			try{
				try{
						Object resource = org.one.stone.soup.browser.Browser.getResource( fileName );
						if(resource != null && resource instanceof Image)
						{
							newImage = (Image)resource;
						}
						else
						{

							newImage = org.one.stone.soup.grfx.ImageFactory.loadImage( org.one.stone.soup.browser.Browser.getInputStream( fileName ) );

							org.one.stone.soup.browser.Browser.storeResource(fileName,(Object)newImage);
						}
				}
				catch(IOException ue)
				{
					System.out.println("Failed to load '"+fileName+"' in SimpleButton");
					//ue.printStackTrace();
				}
			}
			catch(org.one.stone.soup.grfx.ImageLoadException ie)
			{
				ie.printStackTrace();
			}

			return newImage;
}
/**
 * process method comment.
 */
public void process()
{
}
/**
 * register method comment.
 */
public void register(DataLinker store)
{
	imageFile = grfxMLCaster.cast(imageFile,getArg(imageFile,ARG_IMAGE_FILE,store));

	targets = grfxMLCaster.cast(targets,getArg(targets,ARG_TARGETS,store));

}
/**
 * replace method comment.
 */
public void replace(Field oldObj, Field newObj)
{
	imageFile = grfxMLCaster.replace(imageFile,oldObj,newObj);

	targets = grfxMLCaster.replace(targets,oldObj,newObj);
}
/**
 * stop method comment.
 */
public void stop() {}
}
